package thread.java20230108;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class Worker extends Thread{
    BlockingQueue<Runnable> queue = null;
    public Worker(BlockingQueue<Runnable> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        //TODO
        //扫描任务队列, 并执行
        while (true){
            try {
                Runnable runnable = queue.take();
                runnable.run();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
public class MyThreadPool {
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue();

    List<Worker> workerList = new ArrayList<>();

    public MyThreadPool(int corePoolSize){
        for (int i = 0; i < corePoolSize; i++) {
            Worker worker = new Worker(queue);
            worker.start();
            workerList.add(worker);
        }
    }

    public void submit(Runnable runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
